package org.jeecg.modules.lab.items.record.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.lab.entrust.entity.*;
import org.jeecg.modules.lab.entrust.mapper.LabEntrustItemsIndexMapper;
import org.jeecg.modules.lab.entrust.mapper.LabEntrustItemsMapper;
import org.jeecg.modules.lab.entrust.mapper.LabWentrustMapper;
import org.jeecg.modules.lab.entrust.mapper.LabWsuperviseMapper;
import org.jeecg.modules.lab.items.record.mapper.LabItemsRecordReportMapper;
import org.jeecg.modules.lab.items.record.service.ILabItemsRecordReportService;
import org.jeecg.modules.lab.items.vo.LabEntrustItemsIndexPage;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @Description: 项目检测记录_实验报告
 * @Author: jeecg-boot
 * @Date:   2023-10-20
 * @Version: V1.0
 */
@Service
public class LabItemsRecordReportServiceImpl extends ServiceImpl<LabItemsRecordReportMapper, LabItemsRecordReport> implements ILabItemsRecordReportService {

    @Autowired
    private  LabItemsRecordReportMapper labItemsRecordReportMapper;

    @Autowired
    private LabEntrustItemsMapper labEntrustItemsMapper;

    @Autowired
    private LabEntrustItemsIndexMapper labEntrustItemsIndexMapper;

    @Autowired
    private LabWentrustMapper labWentrustMapper;

    @Autowired
    private LabWsuperviseMapper labWsuperviseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMain(LabItemsRecordReport itemsRecordReport, List<LabEntrustItemsIndexPage> labEntrustItemsIndexList, String entrustType) {
        //1、保存项目报告
        labItemsRecordReportMapper.insert(itemsRecordReport);
        //2、更新实验项目指标结论
        if (labEntrustItemsIndexList != null && labEntrustItemsIndexList.size() > 0) {
            for (LabEntrustItemsIndexPage entity : labEntrustItemsIndexList) {
                LabEntrustItemsIndex labEntrustItemsIndex = new LabEntrustItemsIndex();
                BeanUtils.copyProperties(entity, labEntrustItemsIndex);
                labEntrustItemsIndex.setSynFlag("1");
                labEntrustItemsIndexMapper.updateById(labEntrustItemsIndex);
                //更新项目标识
                LabEntrustItems labEntrustItems =labEntrustItemsMapper.selectById(entity.getTaskItemsId());
                labEntrustItems.setSynFlag("1");
                labEntrustItems.setReportMark(entity.getReportMark());
                labEntrustItemsMapper.updateById(labEntrustItems);
            }
        }

        String taskId = itemsRecordReport.getTaskId();
        String reportNum = itemsRecordReport.getReportNum();
        if("委托检验".equals(entrustType)){
            LabWentrust  labWentrust = labWentrustMapper.selectById(taskId);
            labWentrust.setReportCode(reportNum);
            labWentrust.setSynFlag("1");
            labWentrustMapper.updateById(labWentrust);
        }else if("监督检验".equals(entrustType)){
            LabWsupervise labWsupervise = labWsuperviseMapper.selectById(taskId);
            labWsupervise.setReportCode(reportNum);
            labWsupervise.setSynFlag("1");
            labWsuperviseMapper.updateById(labWsupervise);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(LabItemsRecordReport itemsRecordReport, List<LabEntrustItemsIndexPage> labEntrustItemsIndexList,String entrustType) {
        //1、更新项目报告
        itemsRecordReport.setSynFlag("1");
        labItemsRecordReportMapper.updateById(itemsRecordReport);
        //2、更新实验项目指标结论
        if (labEntrustItemsIndexList != null && labEntrustItemsIndexList.size() > 0) {
            for (LabEntrustItemsIndexPage entity : labEntrustItemsIndexList) {
                LabEntrustItemsIndex labEntrustItemsIndex = new LabEntrustItemsIndex();
                BeanUtils.copyProperties(entity, labEntrustItemsIndex);
                labEntrustItemsIndex.setSynFlag("1");
                labEntrustItemsIndexMapper.updateById(labEntrustItemsIndex);
            }
        }
        String taskId = itemsRecordReport.getTaskId();
        String reportNum = itemsRecordReport.getReportNum();
        if("委托检验".equals(entrustType)){
            LabWentrust  labWentrust = labWentrustMapper.selectById(taskId);
            labWentrust.setReportCode(reportNum);
            labWentrust.setSynFlag("1");
            labWentrustMapper.updateById(labWentrust);
        }else if("监督检验".equals(entrustType)){
            LabWsupervise labWsupervise = labWsuperviseMapper.selectById(taskId);
            labWsupervise.setReportCode(reportNum);
            labWsupervise.setSynFlag("1");
            labWsuperviseMapper.updateById(labWsupervise);
        }

    }
}
